a tool for shared writing and social publishing
1import { z } from "zod";
2import { makeRoute } from "../lib";
3import type { Env } from "./route";
4
5export type GetLeafletDataReturnType = Awaited<
6 ReturnType<(typeof get_leaflet_data)["handler"]>
7>;
8
9const leaflets_in_publications_query = `leaflets_in_publications(*, publications(*), documents(*))`;
10const leaflets_to_documents_query = `leaflets_to_documents(*, documents(*))`;
11export const get_leaflet_data = makeRoute({
12 route: "get_leaflet_data",
13 input: z.object({
14 token_id: z.string(),
15 }),
16
17 handler: async ({ token_id }, { supabase }: Pick<Env, "supabase">) => {
18 let res = await supabase
19 .from("permission_tokens")
20 .select(
21 `*,
22 permission_token_rights(*, entity_sets(permission_tokens(${leaflets_in_publications_query}, ${leaflets_to_documents_query}))),
23 custom_domain_routes!custom_domain_routes_edit_permission_token_fkey(*),
24 ${leaflets_in_publications_query},
25 ${leaflets_to_documents_query}`,
26 )
27 .eq("id", token_id)
28 .single();
29
30 type t = typeof res;
31 type data = Exclude<t["data"], null>;
32
33 //All of these shenanigans are to make entity_set optional so that we don't have to write it when we create this data elsewhere, mainly in LeafletList
34 return {
35 result: res as Omit<t, "data"> & {
36 data:
37 | null
38 | (Omit<data, "permission_token_rights"> & {
39 permission_token_rights: (Omit<
40 data["permission_token_rights"][0],
41 "entity_sets"
42 > & {
43 entity_sets?: data["permission_token_rights"][0]["entity_sets"];
44 })[];
45 });
46 },
47 };
48 },
49});